package com.rzico.sbl.ui.mine

import androidx.core.widget.doOnTextChanged
import com.rzico.sbl.R
import com.rzico.sbl.databinding.ActivityWalletDrawBinding
import com.rzico.sbl.model.BaseUrl
import com.rzico.sbl.viewmodel.WalletViewModel
import com.xinnuo.common.extend.content
import com.xinnuo.common.extend.get
import com.xinnuo.common.extend.getExtra
import com.xinnuo.common.extend.oneClick
import com.xinnuo.common.extend.startActivityEx
import com.xinnuo.common.extend.textDouble
import com.xinnuo.common.helper.doubleValue
import com.xinnuo.common.other.DecimalNumberFilter
import com.xinnuo.common.other.NameLengthFilter
import com.xinnuo.common_ui.base.BaseActivity
import com.xinnuo.common_ui.utils.bindLifeCycle
import com.xinnuo.common_ui.utils.subscribeBySuccess

class WalletDrawActivity : BaseActivity(R.layout.activity_wallet_draw) {

    private val mBinding by lazy { ActivityWalletDrawBinding.bind(getRootView()) }

    override fun getViewModel() = get<WalletViewModel>()

    override fun initData() {
        initTitle(title = "提现", name = "提现记录")

        tvRight?.oneClick {
            startActivityEx<WalletDrawRecordActivity>()
        }
    }

    override fun initListener() {
        mBinding.apply {
            walletInput.filters = arrayOf(
                DecimalNumberFilter(),
                NameLengthFilter(8)
            )

            walletInput
                .doOnTextChanged { text, _, _, _ ->
                    walletReal.text = text
                }

            btSure
                .oneClick {
                    val amount = intent.getExtra("amount").doubleValue

                    when {
                        walletReal.textDouble == 0.0 -> {
                            showToast("请输入提现金额")
                            return@oneClick
                        }

                        walletReal.textDouble > amount -> {
                            showToast("可提现金额不足，请重新输入")
                            return@oneClick
                        }
                    }

                    getViewModel()
                        .requestBind(
                            requestUrl = BaseUrl.walletDraw,
                            params = arrayOf(
                                "type" to "weixin",
                                "amount" to walletReal.content
                            )
                        )
                        .bindLifeCycle(getLifecycleOwner())
                        .subscribeBySuccess(
                            event = getViewModel(),
                            onSuccess = {
                                showToast("申请提现提交成功！")
                                finishView()
                            }
                        )
                }
        }
    }

}